package cn.lzd.mr.case7;

import cn.lzd.mr.PathUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lzd on 2018/6/5.
 * join算法 ，将两张表的字段合并，关联字段userid
 * 效率底下
 */
public class Case7MR {

    static class Mp extends Mapper<LongWritable, Text, Text, Bean> {

        String filename;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            FileSplit fileSplit = (FileSplit) context.getInputSplit();
            filename = fileSplit.getPath().getName();
            System.out.println("====================hello=======================" + filename);
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] strs = value.toString().split(",");

            if (filename.indexOf("order") != -1) {
                Bean bean = new Bean(strs[0], strs[1], "null", -1, "null", filename);
                context.write(new Text(strs[1]), bean);
            } else {
                Bean bean = new Bean("null", strs[0], strs[1], Integer.parseInt(strs[2]), strs[3], filename);
                context.write(new Text(strs[0]), bean);
            }
        }
    }

    static class Rd extends Reducer<Text, Bean, Bean, NullWritable> {


        @Override
        protected void reduce(Text key, Iterable<Bean> values, Context context) throws IOException, InterruptedException {
            List<Bean> list = new ArrayList<>();
            Bean newBean = null;
            for (Bean value : values) {
                try {
                    if (value.getFilename().equals("order")) {
                        Bean bean = new Bean();
                        BeanUtils.copyProperties(bean, value);
                        list.add(bean);

                    } else {
                        newBean = new Bean();
                        BeanUtils.copyProperties(newBean, value);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }

            for(Bean b : list){
                b.setAge(newBean.getAge());
                b.setStageName(newBean.getStageName());
                b.setName(newBean.getName());
                context.write(b,NullWritable.get());
            }


        }
    }

    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);

        job.setJarByClass(Case7MR.class);

        job.setMapperClass(Case7MR.Mp.class);
        job.setReducerClass(Case7MR.Rd.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Bean.class);
        job.setOutputKeyClass(Bean.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.addInputPath(job, new Path(PathUtil.getpath("case7") + "/*"));
        FileOutputFormat.setOutputPath(job, new Path(PathUtil.getpath("case7") + "out2/"));


        job.waitForCompletion(true);

    }
}
